/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef NETWORK_ADAPTER_COOLING_H
#define NETWORK_ADAPTER_COOLING_H

#include <mc/core/timer.h>
#include <mc/engine/service.h>

#include <device_tree/interface/NetworkAdapter/Cooling.h>

#include <ncsi_over_mctp/ncsi_over_mctp_mellanox.h>

#define CHIP_TEMP_INVALID 0xff
#define OM_TEMP_SIGN_BIT  0x8000
#define OM_TEMP_DATA_BIT  0x7fff
#define OM_TEMP_INVALID   0x7ffd
#define OM_TEMP_NA        0x7ffe
#define OM_TEMP_ERROR     0x7fff

namespace dev {
using ncsi_over_mctp_mellanox_ptr = std::shared_ptr<ncsi_over_mctp_mellanox>;

class NetworkAdapter_Cooling
    : public mc::engine::interface<NetworkAdapter_Cooling, gen::NetworkAdapter_Cooling> {
public:
    NetworkAdapter_Cooling()  = default;
    ~NetworkAdapter_Cooling() {
        // 释放定时器资源，防止内存泄漏
        stop_ncsi_update_task();
    }

public:
    // ncsi over mctp
    bool update_chip_temp_by_ncsi();
    bool update_max_sfp_temperature(uint16_t max_temperature, uint8_t status);
    void start_ncsi_update_task_mellanox(ncsi_over_mctp_mellanox_ptr ncsi_over_mctp,
                                       mc::milliseconds      interval,
                                       uint8_t package_id );
    void stop_ncsi_update_task();


private:
    mc::core::timer*      m_ncsi_timer = nullptr;
    ncsi_over_mctp_mellanox_ptr m_ncsi_over_mctp_mellanox;
    uint8_t m_package_id = 0;
};

} // namespace dev

#endif // NETWORK_ADAPTER_COOLING_H